<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>09-数据类</title>
  
    
      <meta 
        property="og:title" 
        content="09-数据类">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/09-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E7%9A%84%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E7%9A%84%E5%88%86%E7%B1%BB"><span class="toc-text">变量的类型转换的分类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%98%BE%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">显式类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">隐式类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%88%E7%89%B9%E6%AE%8A%EF%BC%89"><span class="toc-text">隐式类型转换（特殊）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-String"><span class="toc-text">一、转换为 String</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E8%B0%83%E7%94%A8-toString-%E6%96%B9%E6%B3%95"><span class="toc-text">1. 调用 toString()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8-String-%E5%87%BD%E6%95%B0"><span class="toc-text">2. 使用 String()函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5"><span class="toc-text">3. 隐式类型转换：字符串拼接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#prompt-%EF%BC%9A%E7%94%A8%E6%88%B7%E7%9A%84%E8%BE%93%E5%85%A5"><span class="toc-text">prompt()：用户的输入</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-Number"><span class="toc-text">二、转换为 Number</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E4%BD%BF%E7%94%A8-Number-%E5%87%BD%E6%95%B0"><span class="toc-text">1. 使用 Number() 函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E6%AD%A3%E8%B4%9F%E5%8F%B7-a%E3%80%81-a"><span class="toc-text">2. 隐式类型转换：正负号 +a、-a</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8-parseInt-%E5%87%BD%E6%95%B0%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2-gt-%E6%95%B4%E6%95%B0"><span class="toc-text">3. 使用 parseInt()函数：字符串 -&gt; 整数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-parseFloat-%E5%87%BD%E6%95%B0%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2-%E2%80%93-gt-%E6%B5%AE%E7%82%B9%E6%95%B0%EF%BC%88%E5%B0%8F%E6%95%B0%EF%BC%89"><span class="toc-text">4. parseFloat()函数：字符串 –&gt; 浮点数（小数）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-Boolean"><span class="toc-text">三、转换为 Boolean</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BD%AC%E6%8D%A2%E7%BB%93%E6%9E%9C%E5%88%97%E4%B8%BE%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">转换结果列举【重要】</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97"><span class="toc-text">1. 隐式类型转换：逻辑运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8"><span class="toc-text">2. 使用 !!</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8-Boolean-%E5%87%BD%E6%95%B0"><span class="toc-text">3. 使用  Boolean()函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%9A%E5%85%B6%E4%BB%96%E8%BF%9B%E5%88%B6%E7%9A%84%E6%95%B0%E5%AD%97"><span class="toc-text">知识补充：其他进制的数字</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2-1"><span class="toc-text">隐式类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#isNaN-%E5%87%BD%E6%95%B0"><span class="toc-text">isNaN() 函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      09-数据类
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>3.9k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p><strong>变量的数据类型转换</strong>：将一种数据类型转换为另外一种数据类型。</p>
<p>通常有三种形式的类型转换：</p>
<ul>
<li><p>转换为字符串类型</p>
</li>
<li><p>转换为数字型</p>
</li>
<li><p>转换为布尔型</p>
</li>
</ul>
<p>你会专门把某个数据类型转换成 null 或者 undefined 吗？不会，因为这样做，没有意义。</p>
<h2 id="变量的类型转换的分类"><a href="#变量的类型转换的分类" class="headerlink" title="变量的类型转换的分类"></a>变量的类型转换的分类</h2><p>类型转换分为两种：显式类型转换、隐式类型转换。</p>
<h3 id="显式类型转换"><a href="#显式类型转换" class="headerlink" title="显式类型转换"></a>显式类型转换</h3><ul>
<li><p>toString()</p>
</li>
<li><p>String()</p>
</li>
<li><p>Number()</p>
</li>
<li><p>parseInt(string)</p>
</li>
<li><p>parseFloat(string)</p>
</li>
<li><p>Boolean()</p>
</li>
</ul>
<h3 id="隐式类型转换"><a href="#隐式类型转换" class="headerlink" title="隐式类型转换"></a>隐式类型转换</h3><ul>
<li><p>isNaN ()</p>
</li>
<li><p>自增&#x2F;自减运算符：<code>++</code>、<code>—-</code></p>
</li>
<li><p>正号&#x2F;负号：<code>+a</code>、<code>-a</code></p>
</li>
<li><p>加号：<code>+</code></p>
</li>
<li><p>运算符：<code>-</code>、<code>*</code>、<code>/</code></p>
</li>
</ul>
<h3 id="隐式类型转换（特殊）"><a href="#隐式类型转换（特殊）" class="headerlink" title="隐式类型转换（特殊）"></a>隐式类型转换（特殊）</h3><ul>
<li><p>逻辑运算符：<code>&amp;&amp;</code>、<code>||</code>、<code>!</code> 。非布尔值进行<strong>与或</strong>运算时，会先将其转换为布尔值，然后再运算。<code>&amp;&amp;</code>、<code>||</code>的运算结果是<strong>原值</strong>，<code>!</code>的运算结果为布尔值。具体可以看下一篇文章《运算符》。</p>
</li>
<li><p>关系运算符：<code>&lt;</code>、<code>&gt;</code> <code>&lt;=</code> <code>&gt;=</code>等。关系运算符，得到的运算结果都是布尔值：要么是 true，要么是 false。具体可以看下一篇文章《运算符》。</p>
</li>
</ul>
<p>针对上面这两种类型转换，这篇文章来详细介绍。</p>
<h2 id="一、转换为-String"><a href="#一、转换为-String" class="headerlink" title="一、转换为 String"></a>一、转换为 String</h2><h3 id="1-调用-toString-方法"><a href="#1-调用-toString-方法" class="headerlink" title="1. 调用 toString()方法"></a>1. 调用 toString()方法</h3><p>语法：</p>
<pre class="highlight"><span class="line">变量.<span class="title function_">toString</span>();</span><br><span class="line">常量.<span class="title function_">toString</span>(); <span class="comment">// 这里的常量，不允许是数字，但可以是其它常量</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 或者用一个新的变量接收转换结果</span></span><br><span class="line"><span class="keyword">var</span> result = 变量.<span class="title function_">toString</span>();</span><br></pre>

<p>【重要】该方法<strong>不会影响到原变量</strong>，它会将转换的结果返回。当然我们还可以直接写成<code>a = a.toString()</code>，这样的话，就是直接修改原变量。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="comment">// 基本数据类型</span></span><br><span class="line"><span class="keyword">var</span> a1 = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> a2 = <span class="number">29</span>;</span><br><span class="line"><span class="keyword">var</span> a3 = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引用数据类型</span></span><br><span class="line"><span class="keyword">var</span> a4 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> a5 = &#123; <span class="attr">name</span>: <span class="string">&#x27;qianguyihao&#x27;</span>, <span class="attr">age</span>: <span class="number">29</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// null 和 undefined</span></span><br><span class="line"><span class="keyword">var</span> a6 = <span class="literal">null</span>;</span><br><span class="line"><span class="keyword">var</span> a7 = <span class="literal">undefined</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印结果都是字符串</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a1.<span class="title function_">toString</span>()); <span class="comment">// &quot;qianguyihao&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a2.<span class="title function_">toString</span>()); <span class="comment">// &quot;29&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a3.<span class="title function_">toString</span>()); <span class="comment">// &quot;true&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a4.<span class="title function_">toString</span>()); <span class="comment">// &quot;1,2,3&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a5.<span class="title function_">toString</span>()); <span class="comment">// &quot;object&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 下面这两个，打印报错</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a6.<span class="title function_">toString</span>()); <span class="comment">// 报错：Uncaught TypeError: Cannot read properties of null</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a7.<span class="title function_">toString</span>()); <span class="comment">// 报错：Uncaught TypeError: Cannot read properties of undefined</span></span><br></pre>

<p>小技巧：在 chrome 浏览器的控制台中，Number类型、Boolean类型的打印结果是蓝色的，String类型的打印结果是黑色的。</p>
<p>一起来看看 toString() 的注意事项。</p>
<p>（1）null 和 undefined 这两个值没有 toString() 方法，所以它们不能用 toString() 。如果调用，会报错。</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">null</span>.<span class="title function_">toString</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="literal">undefined</span>.<span class="title function_">toString</span>());</span><br></pre>

<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20211116_1458.png" class="lozad post-image"src="https://img.smyhvae.com/20211116_1458.png"></p>
<p>如果你不确定一个值是不是<code>null</code>或<code>undefined</code>，可以使用<code>String()</code>函数，下一小段会讲。</p>
<p>（2）多数情况下，<code>toString()</code>不接收任何参数；当然也有例外：Number 类型的变量，在调用 toString()时，可以在方法中传递一个整数作为参数。此时它会把数字转换为指定的进制，如果不指定则默认转换为 10 进制。例如：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="number">255</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Number数值在调用toString()时，可以在方法中传递一个整数作为参数</span></span><br><span class="line"><span class="comment">//此时它将会把数字转换为指定的进制,如果不指定则默认转换为10进制</span></span><br><span class="line">a = a.<span class="title function_">toString</span>(<span class="number">2</span>); <span class="comment">// 转换为二进制</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// &quot;11111111&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// string</span></span><br></pre>

<p>（3）纯小数的小数点后面，如果紧跟连续6个或6个以上的“0”时，那么，将用e来表示这个小数。代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> num1 = <span class="number">0.000001</span>; <span class="comment">// 小数点后面紧跟五个零</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(num1.<span class="title function_">toString</span>()); <span class="comment">// 打印结果：&quot;0.000001&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> num2 = <span class="number">0.0000001</span>; <span class="comment">// 小数点后面紧跟六个零</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(num2.<span class="title function_">toString</span>()); <span class="comment">// 【重点关注】打印结果：&quot;1e-7&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> num3 = <span class="number">1.0000001</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(num3.<span class="title function_">toString</span>()); <span class="comment">// 打印结果：&quot;1.0000001&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> num4 = <span class="number">0.10000001</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(num4.<span class="title function_">toString</span>()); <span class="comment">// 打印结果：&quot;0.10000001&quot;</span></span><br></pre>

<p>（4）常量可以直接调用 toString() 方法，但这里的常量，不允许是数字。举例如下：</p>
<pre class="highlight"><span class="line"><span class="number">1.</span><span class="title function_">toString</span>(); <span class="comment">// 注意，会报错</span></span><br><span class="line"><span class="number">1.</span>.<span class="title function_">toString</span>(); <span class="comment">// 合法。得到的结果是字符串&quot;1&quot;</span></span><br><span class="line"><span class="number">1.2</span>.<span class="title function_">toString</span>(); <span class="comment">// 合法。得到的结果是字符串&quot;1.2&quot;</span></span><br><span class="line">(<span class="number">1</span>).<span class="title function_">toString</span>(); <span class="comment">// 合法。得到的结果是字符串&quot;1&quot;</span></span><br></pre>

<p>上方代码中，为何出现这样的打印结果？这是因为：</p>
<ul>
<li>第一行代码：JS引擎认为<code>1.toString()</code>中的<code>.</code>是小数点，小数点后面的字符是非法的。</li>
<li>第二行、第三行代码：JS引擎认为第一个<code>.</code>是小数点，第二个<code>.</code>是属性访问的语法，所以能正常解释实行。</li>
<li>第四行嗲吗：用<code>()</code>排除了<code>.</code>被视为小数点的语法解释，所以这种写法也能正常解释执行。</li>
</ul>
<p>参考链接：<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/88570529a03c">你不知道的toString方法</a></p>
<p>（5）既然常量没有方法，那它为什么可以调用 toString()呢？这是因为，除了 null、undefined之外，其他的常量都有对应的特殊的引用类型——<strong>基本包装类型</strong>，所以代码在解释执行的时候，会将常量转为基本包装类型，这样就可以调用相应的引用类型的方法。</p>
<p>我们在后续的内容《JavaScritpt基础&#x2F;基本包装类型》中会专门讲到基本包装类型。</p>
<h3 id="2-使用-String-函数"><a href="#2-使用-String-函数" class="headerlink" title="2. 使用 String()函数"></a>2. 使用 String()函数</h3><p>语法：</p>
<pre class="highlight"><span class="line"><span class="title class_">String</span>(变量/常量);</span><br></pre>

<p>使用 String()函数做强制类型转换时：</p>
<ul>
<li>对于 Number、Boolean、String、Object 而言，本质上就是调用 toString()方法，返回结果同 toString()方法。</li>
<li>但是对于 null 和 undefined，则不会调用 toString()方法。它会将 null 直接转换为 “null”。将 undefined 直接转换为 “undefined”。</li>
</ul>
<p>该方法<strong>不会影响到原数值</strong>，它会将转换的结果返回。</p>
<h3 id="3-隐式类型转换：字符串拼接"><a href="#3-隐式类型转换：字符串拼接" class="headerlink" title="3. 隐式类型转换：字符串拼接"></a>3. 隐式类型转换：字符串拼接</h3><p>格式：变量+”” 或者 变量+”abc”</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="number">123</span>; <span class="comment">// Number 类型</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a + <span class="string">&#x27;&#x27;</span>); <span class="comment">// 打印结果：&quot;123&quot;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a + <span class="string">&#x27;haha&#x27;</span>); <span class="comment">// 打印结果：&quot;123haha&quot;</span></span><br></pre>

<p>上面的例子中，打印的结果，都是字符串类型的数据。实际上底层是调用的 String() 函数。</p>
<h3 id="prompt-：用户的输入"><a href="#prompt-：用户的输入" class="headerlink" title="prompt()：用户的输入"></a>prompt()：用户的输入</h3><p>我们在前面的《JavaScript基础&#x2F;02-JavaScript书写方式：hello world》就讲过，<code>prompt()</code>就是专门用来弹出能够让用户输入的对话框。重要的是：用户不管输入什么，都当字符串处理。</p>
<h2 id="二、转换为-Number"><a href="#二、转换为-Number" class="headerlink" title="二、转换为 Number"></a>二、转换为 Number</h2><h3 id="1-使用-Number-函数"><a href="#1-使用-Number-函数" class="headerlink" title="1. 使用 Number() 函数"></a>1. 使用 Number() 函数</h3><p>语法：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> result = <span class="title class_">Number</span>(变量/常量);</span><br></pre>

<p><strong>情况一：字符串 –&gt; 数字</strong></p>
<p>（1）如果字符串中是纯数字，则直接将其转换为数字。</p>
<p>（2）如果字符串是一个<strong>空串</strong>或者是一个<strong>全是空格</strong>的字符串，则转换为 0。</p>
<p>（3）只要字符串中包含了其他非数字的内容（<code>小数点</code>按数字来算），则转换为 NaN。怎么理解这里的 <strong>NaN</strong> 呢？可以这样理解，使用 Number() 函数之后，<strong>如果无法转换为数字，就会转换为 NaN</strong>。</p>
<p><strong>情况二：布尔 –&gt; 数字</strong></p>
<p>（1）true 转成 1</p>
<p>（2）false 转成 0</p>
<p><strong>情况三：null –&gt; 数字</strong>，结果为：0</p>
<p><strong>情况四：undefined –&gt; 数字</strong>，结果为：NaN</p>
<h3 id="2-隐式类型转换：正负号-a、-a"><a href="#2-隐式类型转换：正负号-a、-a" class="headerlink" title="2. 隐式类型转换：正负号 +a、-a"></a>2. 隐式类型转换：正负号 <code>+a</code>、<code>-a</code></h3><blockquote>
<p>注意，这里说的是正号&#x2F;负号，不是加号&#x2F;减号。</p>
</blockquote>
<p>任何值做<code>+a</code>、<code>-a</code>运算时， 底层调用的是 Number() 函数。不会改变原数值；得到的结果，会改变正负性。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> a1 = <span class="string">&#x27;123&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(+a1); <span class="comment">// 123</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(-a1); <span class="comment">// -123</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a2 = <span class="string">&#x27;123abc&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(+a2); <span class="comment">// NaN</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(-a2); <span class="comment">// NaN</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a3 = <span class="literal">true</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(+a3); <span class="comment">// 1</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(-a3); <span class="comment">// -1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a4 = <span class="literal">false</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(+a4); <span class="comment">// 0</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(-a4); <span class="comment">// -0</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a5 = <span class="literal">null</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(+a5); <span class="comment">// 0</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(-a5); <span class="comment">// -0</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a6 = <span class="literal">undefined</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(+a6); <span class="comment">// NaN</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(-a6); <span class="comment">// NaN</span></span><br></pre>

<h3 id="3-使用-parseInt-函数：字符串-gt-整数"><a href="#3-使用-parseInt-函数：字符串-gt-整数" class="headerlink" title="3. 使用 parseInt()函数：字符串 -&gt; 整数"></a>3. 使用 parseInt()函数：字符串 -&gt; 整数</h3><p>语法：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> result = <span class="built_in">parseInt</span>(需要转换的字符串)</span><br></pre>

<p><strong>parseInt()<strong>：将传入的数据当作</strong>字符串</strong>来处理，从左至右提取数值，一旦遇到非数值就立即停止；停止时如果还没有提取到数值，就返回NaN。</p>
<p>parse 表示“转换”，Int 表示“整数”。例如：</p>
<pre class="highlight"><span class="line"><span class="built_in">parseInt</span>(<span class="string">&#x27;5&#x27;</span>); <span class="comment">// 得到的结果是数字 5</span></span><br></pre>

<p>按照上面的规律，parseInt()的转换结果，列举如下。</p>
<p><strong>情况一：字符串 –&gt; 数字</strong></p>
<p>（1）<strong>只保留字符串最开头的数字</strong>，后面的中文自动消失。</p>
<p>（2）如果字符串不是以数字开头，则转换为 NaN。</p>
<p>（3）如果字符串是一个空串或者是一个全是空格的字符串，转换时会报错。</p>
<p><strong>情况二：Boolean –&gt; 数字</strong>，结果为：NaN</p>
<p><strong>情况三：Null –&gt; 数字</strong>，结果为：NaN</p>
<p><strong>情况四：Undefined –&gt; 数字</strong>，结果为：NaN</p>
<hr>
<p>Number() 函数和 parseInt() 函数的区别：</p>
<p>就拿<code>Number(true)</code> 和 <code>parseInt(true)/parseFloat(true)</code>来举例，二者在使用时，是有区别的：</p>
<ul>
<li><p>Number(true) ：千方百计地想转换为数字；如果转换不了则返回 NaN。</p>
</li>
<li><p>parseInt(true)&#x2F;parseFloat(true) ：提取出最前面的数字部分；没提取出来，那就返回 NaN。</p>
</li>
</ul>
<p><strong>parseInt()具有以下特性</strong>：</p>
<p>（1）parseInt()、parseFloat()会将传入的数据当作<strong>字符串</strong>来处理。也就是说，如果对<strong>非 String</strong>使用 parseInt()、parseFloat()，它会<strong>先将其转换为 String</strong> 然后再操作。【重要】</p>
<p>比如：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="number">168.23</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(a)); <span class="comment">//打印结果：168  （因为是先将 a 转为字符串&quot;168.23&quot;，然后然后再操作）</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = <span class="literal">true</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(b)); <span class="comment">//打印结果：NaN （因为是先将 b 转为字符串&quot;true&quot;，然后然后再操作）</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = <span class="literal">null</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(c)); <span class="comment">//打印结果：NaN  （因为是先将 c 转为字符串&quot;null&quot;，然后然后再操作）</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> d = <span class="literal">undefined</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(d)); <span class="comment">//打印结果：NaN  （因为是先将 d 转为字符串&quot;undefined&quot;，然后然后再操作）</span></span><br></pre>


<p>（2）<strong>只保留字符串最开头的数字</strong>，后面的中文自动消失。例如：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(<span class="string">&#x27;2017在公众号上写了6篇文章&#x27;</span>)); <span class="comment">//打印结果：2017</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(<span class="string">&#x27;2017.01在公众号上写了6篇文章&#x27;</span>)); <span class="comment">//打印结果仍是：2017   （说明只会取整数）</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseInt</span>(<span class="string">&#x27;aaa2017.01在公众号上写了6篇文章&#x27;</span>)); <span class="comment">//打印结果：NaN （因为不是以数字开头）</span></span><br></pre>


<p>（3）自动截断小数：<strong>取整，不四舍五入</strong>。</p>
<p>例 1：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="built_in">parseInt</span>(<span class="number">5.8</span>) + <span class="built_in">parseInt</span>(<span class="number">4.7</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a);</span><br></pre>

<p>打印结果：</p>
<pre class="highlight"><span class="line">9</span><br></pre>

<p>例 2：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="built_in">parseInt</span>(<span class="number">5.8</span> + <span class="number">4.7</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a);</span><br></pre>

<p>打印结果：</p>
<pre class="highlight"><span class="line"><span class="number">10</span>;</span><br></pre>

<p>（4）带两个参数时，表示在转换时，包含了进制转换。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;110&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> num = <span class="built_in">parseInt</span>(a, <span class="number">16</span>); <span class="comment">// 【重要】将 a 当成 十六进制 来看待，转换成 十进制 的 num</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(num);</span><br></pre>

<p>打印结果：</p>
<pre class="highlight"><span class="line">272</span><br></pre>

<p>如果你对打印结果感到震惊，请仔细看上面的代码注释。就是说，无论 parseInt() 里面的进制参数是多少，最终的转换结果是十进制。</p>
<p>我们来看下面的代码，打印结果继续震惊。</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;5&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> num = <span class="built_in">parseInt</span>(a, <span class="number">2</span>); <span class="comment">// 将 a 当成 二进制 来看待，转换成 十进制 的 num</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(num); <span class="comment">// 打印结果：NaN。因为 二进制中没有 5 这个数，转换失败。</span></span><br></pre>

<h3 id="4-parseFloat-函数：字符串-–-gt-浮点数（小数）"><a href="#4-parseFloat-函数：字符串-–-gt-浮点数（小数）" class="headerlink" title="4. parseFloat()函数：字符串 –&gt; 浮点数（小数）"></a>4. parseFloat()函数：字符串 –&gt; 浮点数（小数）</h3><p>parseFloat()的作用是：将字符串转换为<strong>浮点数</strong>。</p>
<p>parseFloat()和 parseInt()的作用类似，不同的是，parseFloat()可以获得小数部分。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;123.456.789px&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">parseFloat</span>(a)); <span class="comment">// 打印结果：123.456</span></span><br></pre>

<p>parseFloat() 的几个特性，可以参照 parseInt()。</p>
<h2 id="三、转换为-Boolean"><a href="#三、转换为-Boolean" class="headerlink" title="三、转换为 Boolean"></a>三、转换为 Boolean</h2><h3 id="转换结果列举【重要】"><a href="#转换结果列举【重要】" class="headerlink" title="转换结果列举【重要】"></a>转换结果列举【重要】</h3><p>其他的数据类型都可以转换为 Boolean 类型。无论是隐式转换，还是显示转换，转换结果都是一样的。有下面几种情况：</p>
<p>（1）情况一：数字 –&gt; 布尔。 0 和 NaN的转换结果 false，其余的都是 true。比如 <code>Boolean(NaN)</code>的结果是 false。</p>
<p>（2）情况二：字符串 —&gt; 布尔。空串的转换结果是false，其余的都是 true。全是空格的字符串，转换结果也是 true。字符串<code>&#39;0&#39;</code>的转换结果也是 true。</p>
<p>（3）情况三：null 和 undefined 都会转换为 false。</p>
<p>（4）情况四：引用数据类型会转换为 true。注意，空数组<code>[]</code>和空对象<code>&#123;&#125;</code>，<strong>转换结果也是 true</strong>，这一点，很多人都不知道。</p>
<p><strong>重中之重来了：</strong></p>
<p>转换为 Boolean 的上面这几种情况，<strong>极其重要</strong>，开发中会频繁用到。比如说，我们在项目开发中，经常需要对一些<strong>非布尔值</strong>做逻辑判断，符合条件后，才做下一步的事情。这个逻辑判断就是依据上面的四种情况。</p>
<p>举例：（接口返回的内容不为空，前端才做进一步的事情）</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> result1 = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> result2 = &#123; <span class="attr">a</span>: <span class="string">&#x27;data1&#x27;</span>, <span class="attr">b</span>: <span class="string">&#x27;data2&#x27;</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (result1) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;因为 result1的内容为空，所以代码进不了这里&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (result2 &amp;&amp; result2.<span class="property">a</span>) &#123;</span><br><span class="line">    <span class="comment">// 接口返回了 result2，且 result2.a 里面有值，前端才做进一步的事情</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;代码能进来，前端继续在这里干活儿&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre>

<p>这里再次强调一下，空数组<code>[]</code>和空对象<code>&#123;&#125;</code>转换为 Boolean 值时，转换结果为 true。</p>
<h3 id="1-隐式类型转换：逻辑运算"><a href="#1-隐式类型转换：逻辑运算" class="headerlink" title="1. 隐式类型转换：逻辑运算"></a>1. 隐式类型转换：逻辑运算</h3><p>当非 Boolean 类型的数值和 Boolean 类型的数值做比较时，会先把前者<strong>临时</strong>进行隐式转换为 Boolean 类型，然后再做比较；且不会改变前者的数据类型。举例如下：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> a = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a == <span class="literal">true</span>); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> a); <span class="comment">// 打印结果：number。可见，上面一行代码里，a 做了隐式类型转换，但是 a 的数据类型并没有发生变化，仍然是 Number 类型</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">0</span> == <span class="literal">true</span>); <span class="comment">// 打印结果：false</span></span><br></pre>

<h3 id="2-使用"><a href="#2-使用" class="headerlink" title="2. 使用 !!"></a>2. 使用 <code>!!</code></h3><p>使用 <code>!!</code>可以显式转换为 Boolean 类型。比如 <code>!!3</code>的结果是 true。</p>
<h3 id="3-使用-Boolean-函数"><a href="#3-使用-Boolean-函数" class="headerlink" title="3. 使用  Boolean()函数"></a>3. 使用  Boolean()函数</h3><p>使用 Boolean()函数可以显式转换为 Boolean 类型。</p>
<h2 id="知识补充：其他进制的数字"><a href="#知识补充：其他进制的数字" class="headerlink" title="知识补充：其他进制的数字"></a>知识补充：其他进制的数字</h2><ul>
<li><p>16 进制的数字，以<code>0x</code>开头</p>
</li>
<li><p>8 进制的数字，以<code>0</code>开头</p>
</li>
<li><p>2 进制的数字，<code>0b</code>开头（不是所有的浏览器都支持：chrome 和火狐支持，IE 不支持）</p>
</li>
</ul>
<p>比如<code>070</code>这个字符串，如果我调用 parseInt()转成数字时，有些浏览器会当成 8 进制解析，有些会当成 10 进制解析。</p>
<p>所以，比较建议的做法是：可以在 parseInt()中传递第二个参数，来指定当前数字的进制。例如：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;070&#x27;</span>;</span><br><span class="line"></span><br><span class="line">a = <span class="built_in">parseInt</span>(a, <span class="number">8</span>); <span class="comment">//将 070 当成八进制来看待，转换结果为十进制。</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a); <span class="comment">// 打印结果：56。这个地方要好好理解。</span></span><br></pre>

<h2 id="隐式类型转换-1"><a href="#隐式类型转换-1" class="headerlink" title="隐式类型转换"></a>隐式类型转换</h2><p>重点：<strong>隐式类型转换，内部调用的都是显式类型的方法</strong>。</p>
<p>常见的隐式类型转换，包括下面这几种：</p>
<ul>
<li><p>isNaN() 函数</p>
</li>
<li><p>运算符：加号 <code>+</code></p>
</li>
<li><p>运算符：<code>-</code>、<code>*</code>、<code>/</code>、<code>%</code></p>
</li>
<li><p>运算符：正号&#x2F;负号 +a<code>、</code>-a&#96;</p>
</li>
<li><p>自增&#x2F;自减运算符：<code>++</code>、<code>—-</code></p>
</li>
</ul>
<p>本段先来讲一下 isNaN() ，其他内容，将在下一节《JavaScript基础&#x2F;运算符》中讲到。</p>
<h3 id="isNaN-函数"><a href="#isNaN-函数" class="headerlink" title="isNaN() 函数"></a>isNaN() 函数</h3><p>语法：</p>
<pre class="highlight"><span class="line"><span class="built_in">isNaN</span>(参数);</span><br></pre>

<p>解释：判断指定的参数是否为 NaN（非数字类型），返回结果为 Boolean 类型。也就是说：<strong>任何不能被转换为数值的参数，都会让这个函数返回 true</strong>。</p>
<p><strong>执行过程</strong>：</p>
<p>（1）先调用<code>Number(参数)</code>函数；</p>
<p>（2）然后将<code>Number(参数)</code>的返回结果是否为数值。如果不为数值，则最终结果为 true；如果为数值，则最终结果为 false。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="string">&#x27;123&#x27;</span>)); <span class="comment">// 返回结果：false。</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="literal">null</span>)); <span class="comment">// 返回结果：false</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="string">&#x27;abc&#x27;</span>)); <span class="comment">// 返回结果：true。因为 Number(&#x27;abc&#x27;) 的返回结果是 NaN</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="literal">undefined</span>)); <span class="comment">// 返回结果：true</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="built_in">isNaN</span>(<span class="title class_">NaN</span>)); <span class="comment">// 返回结果：true</span></span><br></pre>

<h2 id="赞赏作者"><a href="#赞赏作者" class="headerlink" title="赞赏作者"></a>赞赏作者</h2><p>创作不易，你的赞赏和认可，是我更新的最大动力：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20220401_1800.jpg" class="lozad post-image"src="https://img.smyhvae.com/20220401_1800.jpg"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/09-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/09-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/07-%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%9ANumber/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">07-基本数据类型：Number </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/12-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5%EF%BC%9A%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84%EF%BC%88for%E5%92%8Cwhile%EF%BC%89/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">12-流程控制语句：循环结构（for和while） </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E7%9A%84%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E7%9A%84%E5%88%86%E7%B1%BB"><span class="toc-text">变量的类型转换的分类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%98%BE%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">显式类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">隐式类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%88%E7%89%B9%E6%AE%8A%EF%BC%89"><span class="toc-text">隐式类型转换（特殊）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-String"><span class="toc-text">一、转换为 String</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E8%B0%83%E7%94%A8-toString-%E6%96%B9%E6%B3%95"><span class="toc-text">1. 调用 toString()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8-String-%E5%87%BD%E6%95%B0"><span class="toc-text">2. 使用 String()函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5"><span class="toc-text">3. 隐式类型转换：字符串拼接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#prompt-%EF%BC%9A%E7%94%A8%E6%88%B7%E7%9A%84%E8%BE%93%E5%85%A5"><span class="toc-text">prompt()：用户的输入</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-Number"><span class="toc-text">二、转换为 Number</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E4%BD%BF%E7%94%A8-Number-%E5%87%BD%E6%95%B0"><span class="toc-text">1. 使用 Number() 函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E6%AD%A3%E8%B4%9F%E5%8F%B7-a%E3%80%81-a"><span class="toc-text">2. 隐式类型转换：正负号 +a、-a</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8-parseInt-%E5%87%BD%E6%95%B0%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2-gt-%E6%95%B4%E6%95%B0"><span class="toc-text">3. 使用 parseInt()函数：字符串 -&gt; 整数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-parseFloat-%E5%87%BD%E6%95%B0%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2-%E2%80%93-gt-%E6%B5%AE%E7%82%B9%E6%95%B0%EF%BC%88%E5%B0%8F%E6%95%B0%EF%BC%89"><span class="toc-text">4. parseFloat()函数：字符串 –&gt; 浮点数（小数）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-Boolean"><span class="toc-text">三、转换为 Boolean</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BD%AC%E6%8D%A2%E7%BB%93%E6%9E%9C%E5%88%97%E4%B8%BE%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">转换结果列举【重要】</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97"><span class="toc-text">1. 隐式类型转换：逻辑运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8"><span class="toc-text">2. 使用 !!</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8-Boolean-%E5%87%BD%E6%95%B0"><span class="toc-text">3. 使用  Boolean()函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%9A%E5%85%B6%E4%BB%96%E8%BF%9B%E5%88%B6%E7%9A%84%E6%95%B0%E5%AD%97"><span class="toc-text">知识补充：其他进制的数字</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2-1"><span class="toc-text">隐式类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#isNaN-%E5%87%BD%E6%95%B0"><span class="toc-text">isNaN() 函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E7%9A%84%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E7%9A%84%E5%88%86%E7%B1%BB"><span class="toc-text">变量的类型转换的分类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%98%BE%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">显式类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">隐式类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%88%E7%89%B9%E6%AE%8A%EF%BC%89"><span class="toc-text">隐式类型转换（特殊）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-String"><span class="toc-text">一、转换为 String</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E8%B0%83%E7%94%A8-toString-%E6%96%B9%E6%B3%95"><span class="toc-text">1. 调用 toString()方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8-String-%E5%87%BD%E6%95%B0"><span class="toc-text">2. 使用 String()函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8B%BC%E6%8E%A5"><span class="toc-text">3. 隐式类型转换：字符串拼接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#prompt-%EF%BC%9A%E7%94%A8%E6%88%B7%E7%9A%84%E8%BE%93%E5%85%A5"><span class="toc-text">prompt()：用户的输入</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-Number"><span class="toc-text">二、转换为 Number</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E4%BD%BF%E7%94%A8-Number-%E5%87%BD%E6%95%B0"><span class="toc-text">1. 使用 Number() 函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E6%AD%A3%E8%B4%9F%E5%8F%B7-a%E3%80%81-a"><span class="toc-text">2. 隐式类型转换：正负号 +a、-a</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8-parseInt-%E5%87%BD%E6%95%B0%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2-gt-%E6%95%B4%E6%95%B0"><span class="toc-text">3. 使用 parseInt()函数：字符串 -&gt; 整数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-parseFloat-%E5%87%BD%E6%95%B0%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2-%E2%80%93-gt-%E6%B5%AE%E7%82%B9%E6%95%B0%EF%BC%88%E5%B0%8F%E6%95%B0%EF%BC%89"><span class="toc-text">4. parseFloat()函数：字符串 –&gt; 浮点数（小数）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E8%BD%AC%E6%8D%A2%E4%B8%BA-Boolean"><span class="toc-text">三、转换为 Boolean</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BD%AC%E6%8D%A2%E7%BB%93%E6%9E%9C%E5%88%97%E4%B8%BE%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">转换结果列举【重要】</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%EF%BC%9A%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97"><span class="toc-text">1. 隐式类型转换：逻辑运算</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8"><span class="toc-text">2. 使用 !!</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8-Boolean-%E5%87%BD%E6%95%B0"><span class="toc-text">3. 使用  Boolean()函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E8%A1%A5%E5%85%85%EF%BC%9A%E5%85%B6%E4%BB%96%E8%BF%9B%E5%88%B6%E7%9A%84%E6%95%B0%E5%AD%97"><span class="toc-text">知识补充：其他进制的数字</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2-1"><span class="toc-text">隐式类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#isNaN-%E5%87%BD%E6%95%B0"><span class="toc-text">isNaN() 函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
